package com.jiangyao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jiangyao.common.ApiResult;
import com.jiangyao.common.annotation.Cache;
import com.jiangyao.common.utils.DateUtil;
import com.jiangyao.component.RedisComponent;
import com.jiangyao.model.dto.DemoDto;
import com.jiangyao.model.dto.DemoListPageDTO;
import com.jiangyao.model.entity.Demo;
import com.jiangyao.model.vo.ListPageVO;
import com.jiangyao.model.vo.UserSessionVO;
import com.jiangyao.service.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * demo表熟悉框架 前端控制器
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/demo")
@Api(tags = "测试Demo接口")
public class DemoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

	@Autowired
	private DemoService demoService;

	@Autowired
	private RedisComponent redisComponent;

	@Autowired
	private DozerBeanMapper dozerBeanMapper;

	/**
	 * 列表
	 *
	 * @return 结果
	 */
	@GetMapping("list")
	@Cache(expire = 300)
	@ApiOperation(value = "获取demo列表")
	public List<Demo> getList() {
		QueryWrapper<Demo> queryWrapper = new QueryWrapper<>();
		List<Demo> list = demoService.list(queryWrapper);
		redisComponent.set("test:demo:list", list, 600);
		return list;
	}


	/**
	 * 列表
	 *
	 * @return 结果
	 */
	@GetMapping("listCache")
	public ApiResult<List<Demo>> getListCache() {
		List<Demo> list = redisComponent.getListObject("test:demo:list");
		return new ApiResult<>(list);
	}


	/**
	 * 添加demo
	 *
	 * @return 结果
	 */
	@GetMapping("add")
	@ApiOperation(value = "添加demo")
	public ApiResult<Demo> addDemo(@RequestParam String userName) {
		Demo demo = new Demo();
		demo.setUserId(1L);
		demo.setStFlpv("aaa");
		demo.setUserName(userName);
		demo.setCurrentMode(1);
		demo.setCreatedAt(new Date());
		demo.setUpdatedAt(new Date());
		demoService.save(demo);
		return new ApiResult<>(demo);
	}

	/**
	 * 添加demo
	 *
	 * @return 结果
	 */
	@PostMapping("addJson")
	@ApiOperation("添加addDemoJson")
	public Demo addDemoJson(@RequestBody DemoDto demoDto) {
		Demo demo = dozerBeanMapper.map(demoDto, Demo.class);
		demo.setCreatedAt(new Date());
		demo.setUpdatedAt(new Date());
		demoService.save(demo);
		return demo;
	}

	/**
	 * demo分页查询
	 * @param demoListPageDTO 分页请求参数
	 * @return 结果
	 */
	@PostMapping("demoPage")
	@ApiOperation("demo分页查询")
	public ApiResult<ListPageVO<Demo>> demoPage(@Validated() @RequestBody DemoListPageDTO demoListPageDTO) {
		Page<Demo> page = demoService.page(demoListPageDTO);
		return new ApiResult(page);
	}


	/**
	 * 时间
	 *
	 * @return 结果
	 */
	@GetMapping("date")
	public String getDate() {
		String result = new Date().toString();
		result = result + "===" + DateUtil.format(new Date(1592639239000L), DateUtil.DTF_YMDHMSS_24);
		result = result + "===" + DateUtil.format(null, DateUtil.DTF_YMDHMSS_24);
		return result;
	}

	private final static String key = "temp:test:";

	private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 10,
			TimeUnit.MINUTES, new LinkedBlockingQueue<>(),
			new ThreadFactoryBuilder().setNameFormat("-redis-pool-%d-").build());


	@GetMapping("addRedis")
	public ApiResult<String> add() throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			executor.execute(this::addThread);
		}
		return new ApiResult<>();
	}

	private void addThread() {
		String threadName = Thread.currentThread().getName();
		for (int i = 0; i < 100000; i++) {
			LOGGER.info(threadName + "----------------------------增加中");
			UserSessionVO u = UserSessionVO.builder()
					.userName("zhansan" + i).openId("aasdddddddddddddd").userPhone("13888888888").token("asdddddddddewqfrqrfsavagbwerbeqnb")
					.build();
			redisComponent.set(key + threadName + ":" + UUID.randomUUID().toString(), u, 3600);

			if (i % 100 == 0) {
				LOGGER.info(threadName + "----------------------------增加中，每100条完成");
			}
		}
		LOGGER.info("----------------------------增加完成");
	}


}

